$(function(){
	active();
	findType();

});
function findType(){
	$("#typeId").html("");
	var url="../../comtype/query.action";
	var data=null;
	$.post(url,data,function(mes){
		$("#typeId").append("<option value=''>====请选择====</option>");
		$.each(mes,function(index,item){
			$("#typeId").append("<option value="+item.typeId+">"+item.typeName+"</option>");
			$("#typeSelect").append("<option value="+item.typeId+">"+item.typeName+"</option>");
			load();
		});
	},"json");
}

layui.use(['form', 'layedit', 'laydate','table'], function(){
	var table = layui.table
	,laydate = layui.laydate
	,form = layui.form
	,layer = layui.layer
	,layedit = layui.layedit;

	
	table.on('checkbox(insertTable)', function(obj){
		var arr=table.cache.insertTable;
		if(obj.type=="one"){
			console.info("单选"+obj.data);

			if(obj.checked){
				$("#comIdInput").val($("#comIdInput").val()+obj.data.comId+"@");
			}else{
				var arr=$("#comIdInput").val().split("@");
				var it="";
				$("#comIdInput").val("");
				$.each(arr,function(index,item){
					if(item!=obj.data.comId && item !=null && item!='' ){
						$("#comIdInput").val($("#comIdInput").val()+item+"@");
					}
				});
			}
		}else if(obj.type=="all"){
			
			if(obj.checked){
				console.info("全选选中");
				$.each(arr,function(index,item){
						var state=0;
						$.each($("#comIdInput").val().split("@"),function(i,info){
							if(info==item.comId){
								state=1;
							}
						});
						if(state==0){
							$("#comIdInput").val($("#comIdInput").val()+item.comId+"@");
						}
				});
					
			}else{
				console.info("全选取消");
				$.each(arr,function(index,item){
						var arrs="";
						$.each($("#comIdInput").val().split("@"),function(i,info){
							if(info!=item.comId && info!='' && info!=null){
								arrs+=info+"@";
							}
						});
						$("#comIdInput").val(arrs);
						
				});
			}
		}

		

		setTimeout(function(){
			$.each($("#comIdInput").val().split("@"),function(index,item){
				$("#insertFootDiv").html("<ul></ul>");
				var urls="../../commodity/findById.action";
				var datas={"comId":item};
				$.post(urls,datas,function(info){
					//$("#insertFootDiv").append("<span style='border:1px solid red;' >"+info.comName+"<input type='button' value='关闭' id="+info.comId+" onclick='findCheckBox(\""+info.comId+"\",\""+info.comName+"\");'/></span>"+"   ");
					$("#insertFootDiv ul").append("<li id="+info.comId+">"
												+"<a href='javascript:findCheckBox(\""+info.comId+"\",\""+info.comName+"\");'>"
													+info.comName
												+"</a>"
											+"</li>");
				},"json");
			});
			
		},500);
		
	

	});

	//监听工具条
	table.on('tool(demo)', function(obj){
		var da = obj.data;
		if(obj.event === 'del'){
			layer.confirm('真的删除行么', function(index){
				var index = layer.load(0, {shade: false});
				var url="../../prodetail/delete.action";
				var data={"deId":da.deId}
				$.post(url,data,function(mes){
					layer.close(index);
					if(mes==1){
						obj.del();
						layer.close(index);
						layer.msg("删除成功");
						/*table.reload('idTest', {
							page: {
								curr: 1 //重新从第 1 页开始
							}
						,where: {
							proId: proId,
						}
						});*/
						
					}else{
						layer.msg("删除失败");
					}
				},"json");

			});
		}


	});



	var $ = layui.$, active = {
			getCheckData: function(){
				//得到idTest表的所有列的数据
				console.info(table.cache.idTest);
				//获取选中数据
				//var checkStatus = table.checkStatus('idTest')
				//,data = checkStatus.data;
				//layer.alert(JSON.stringify(data));

				var arr=table.cache.idTest;
				if(arr.length==0){
					layer.msg("请先点击增加添加商品");
				}else{
					var index = layer.load(0, {shade: false});
					var sum="";
					$.each(arr,function(index,item){
						console.info(item.deId);
						console.info(item.comName);
						console.info(item.deNumber);
						sum+=item.deId+","+item.deNumber+","+item.comId+"@"
					});
					var url="../../prodetail/updateNumber.action";
					var data={"sum":sum};
					$.post(url,data,function(mes){
						layer.close(index);
						if(mes.state==1){
							layer.msg('保存成功', {
								icon: 1
							});
						}else if(mes.state==0){
							layer.msg('保存失败', {
								icon: 2
							});
						}else{
							layer.msg(mes.msg, {
								icon: 2
							});
						}
					},"json");

				}
			}
	,getAdd:function(){
		//获取选中数据
		//var checkStatus = table.checkStatus('insertTable')
		//,data = checkStatus.data;
		//eval("var tabArr="+JSON.stringify(data));
		if($("#comIdInput").val().length==0){
			layer.msg("请选中要添加的商品");
		}else{
			var index = layer.load(0, {shade: false});
			var proId=getParameter("proId");
			var url="../../prodetail/findAdd.action";
			var data={"com":$("#comIdInput").val(),"proId":proId};
			$.post(url,data,function(mes){
				layer.close(index);
				if(mes.state==1){
					layer.msg('添加成功', {
						icon: 1
					});
					layer.closeAll('page');
					$("#insertCom").css({"display":"none"});
					table.reload('idTest', {
						page: {
							curr: 1 //重新从第 1 页开始
						}
					,where: {
						proId: proId,
					}
					});
				}else if(mes.state==2){
					layer.msg('添加失败,'+mes.msg, {
						icon: 2
					});
				}else{
					layer.msg('添加失败', {
						icon: 2
					});
				}
			});
		}
	}
	,reload: function(){
		var comName = $('#comName');
		var typeId = $('#typeId');
		//执行重载
		table.reload('insertTable', {
			page: {
				curr: 1 //重新从第 1 页开始
			}
		,where: {
			typeId: typeId.val(),
			comName: comName.val(),
		}
		});
	}

	};

	$('.demoTable #selectButton').on('click', function(){
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
		
	});

	$('.demoTable #addButton').on('click', function(){
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});

	$('#insert').on('click', function(){

		layer.open({
			type: 1	 //此处以iframe举例
			,title:false
			,closeBtn :0
			,area:['1000px','450px']
		,offset: 'auto'
			,content:$('#insertCom')
			,success: function(layero){
				//调用默认选中的方法
				findSuccess();
			}
		});
	});
	$('#exit').on('click', function(){
		layer.closeAll('page');
		$("#insertCom").css({"display":"none"});
	});
});

function findSuccess(){

	layui.use('table', function(){
		var table = layui.table;
		
		 
		
		$.ajaxSetup({
			async: false
		});
		//执行重载
		table.reload('insertTable', {
			page: {
				curr: 1 //重新从第 1 页开始
			}
		});
		$("#comIdInput").val("");
		//弹出框回调函数 判断该商品在详情表中是否添加
		var url="../../prodetail/findProId.action";
		var data={"proId":proId};
		$.post(url,data,function(mes){
			$.each(mes,function(index,item){
				console.info(item.comId+"-----"+item.deId);
				$("#comIdInput").val($("#comIdInput").val()+item.comId+"@");
			});
		},"json");
		
		

		setTimeout(function(){
			
			$.each($("#comIdInput").val().split("@"),function(index,item){
				$("#insertFootDiv").html("<ul></ul>");
				var urls="../../commodity/findById.action";
				var datas={"comId":item};
				$.post(urls,datas,function(info){
					//$("#insertFootDiv").append("<span style='border:1px solid red;'>"+info.comName+"<input type='button' value='关闭' id="+info.comId+" onclick='findCheckBox(\""+info.comId+"\",\""+info.comName+"\");'/><span>"+"   ");
					$("#insertFootDiv ul").append("<li id="+info.comId+">"
							+"<a href='javascript:findCheckBox(\""+info.comId+"\",\""+info.comName+"\");'>"
								+info.comName
							+"</a>"
						+"</li>");
				},"json");
			});
			
			//$("#insertFootDiv").html("");
			$.each($(".layui-table-col-special"),function(index,item){
				var comId=$(item).next().next().children().html();
				if(comId!=undefined && comId!=''){
					$.each($("#comIdInput").val().split("@"),function(indexs,items){
						if(items==($(item).next().next().children().html())){
							console.info($(item).children().children(":first"));
							var check=$(item).children().children(":first").get(0);
							$(check).attr({"checked":true});
							load();
						}
					});
				};

			});
		},500);



		$.ajaxSetup({
			async: true
		});

	});

}

function findCheckBox(comId,comName){
	$("#"+comId).remove();
	var arr=$("#comIdInput").val().split("@");
	var it="";
	$("#comIdInput").val("");
	$.each(arr,function(index,item){
		if(item!=comId && item !=null && item!='' ){
			$("#comIdInput").val($("#comIdInput").val()+item+"@");
		}
	});
	$.each($(".layui-table-col-special"),function(index,item){
		var name=$(item).next().next().next().children().html();
		if(name!=undefined && name!=''){
			//alert(name);
			if(name==comName){
				console.info($(item).children().children(":first"));
				var check=$(item).children().children(":first").get(0);
				$(check).attr({"checked":false});
				load();
			}
		};
	});
}
var proId;
function active(){
	proId=getParameter("proId");
	layui.use('table', function(){
		var table = layui.table;
		var $ = layui.$, active = {
				reload: function(){
					//var proId = $('#proId');
					//执行重载
					table.reload('idTest', {
						page: {
							curr: 1 //重新从第 1 页开始
						}
					,where: {
						proId: proId,
					}
					});
				}
		};

		active['reload'] ? active['reload'].call(this) : '';
	});
}

function getParameter(param)
{
	var query = window.location.search;
	var iLen = param.length;
	var iStart = query.indexOf(param);
	if (iStart == -1)
		return "";
	iStart += iLen + 1;
	var iEnd = query.indexOf("&", iStart);
	if (iEnd == -1)
		return query.substring(iStart);

	return query.substring(iStart, iEnd);
}
function load() {
	var form = layui.form
	form.render();
}